<template>
  <div>
    <el-dialog
      title="添加大题"
      :visible.sync="addBigQuestionDialog"
      width="40%"
      :before-close="dialogNone"
      class="dialog">
      <div class="content">
        <el-form :rules="reactedRules" :model="basicForm" ref="basicForm">
          <el-form-item label="大题序号" prop="questionNum">
            <el-input-number v-model="basicForm.questionNum" :min="1" label="请输入大题序号"></el-input-number>
          </el-form-item>
          <el-form-item label="大题题型" prop="typeValue">
            <el-select v-model="basicForm.typeValue" placeholder="请选择题型" clearable>
              <el-option
                v-for="item in type"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle('basicForm')">取 消</el-button>
        <el-button type="primary" @click="save('basicForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ExamMessage from '@/api/exam/examMessage'
export default {
  name: 'AddBigQuestion',
  inject:['reload'],
  data(){
    return {
      basicForm: {
        questionNum: 1,
        typeValue: null,
      },
      type: [
        {value: 1,label: '单选'},
        {value: 2,label: '多选'},
        {value: 3,label: '判断'}
      ],
      reactedRules: {
        questionNum: [
          { required: true, message: '请填写大题序号', trigger: 'blur' }
        ],
        typeValue: [
          { required: true, message: '请选择题型', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    addBigQuestionDialog: {
      type: Boolean,
      default: false
    },
    examId: {
      type: Number
    }
  },
  methods: {
    dialogNone(){
      this.$emit("letDialogNone")
    },
    save(basicForm){
      this.$refs[basicForm].validate((valid) => {
        if(valid){
          // this.$emit('addBigQuestionValue',this.basicForm.questionNum,this.basicForm.typeValue)
          // // 兄弟组件传值，传给rule组件
          // bus.$emit("addBigQuestionValue",this.basicForm.questionNum,this.basicForm.typeValue)

          let params = {
            bigSort: this.basicForm.questionNum,
            quesType: this.basicForm.typeValue,
            examInfoId: this.examId
          }
          ExamMessage.eBigQues(params).then(res=>{
            if(res.code === 0){
              this.$message({
                type: 'success',
                message: '添加成功'
              });
              this.$emit("letDialogNone")
              this.$refs[basicForm].resetFields();
              // 添加成功后调用获取所有数据的接口
              ExamMessage.eExamConfigGet(this.examId).then(res=>{
                if(res.code === 0){
                  this.$emit("updateQues",res.data)
                  console.log(res.data,'ques')
                }
              })
            }else{
              this.$message({
                type: 'error',
                message: '添加失败'
              });
            }
          })
          
        }else{
          return false
        }
      })
    },
    cancle(basicForm){
      this.$message({
        type: 'info',
        message: '已取消配置'
      });
      this.$emit("letDialogNone")
      this.$refs[basicForm].resetFields();
    }
  }
 };
</script>

<style scoped>
  .content{
    margin: 0px 0 0px 20px;
  }
  div>>>.el-dialog{
    margin-top: 25vh !important;
  }
  div>>>.el-dialog__header{
    text-align: center;
  }
</style>